/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.escom.hibernate.template;


import com.escom.cliente.exception.DAOException;
import java.util.Collection;
import org.apache.log4j.Logger;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author yamil
 */
public abstract  class AdminSoportHibernate extends HibernateDaoSupport {
   
    private static Logger logger = Logger.getLogger(AdminSoportHibernate.class);



	public List findAll(Class clazz) throws DAOException {
		try {

			DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
			List list = getHibernateTemplate().findByCriteria(criteria);
			logger.debug("findAll(" + clazz + "):" + list);
			return list;
		}
		catch (HibernateException e) {
			throw new DAOException(e.getMessage(), e);
		}
	}

	public Object findByPrimaryKey(Class clazz, Object id) throws DAOException {
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
			criteria.add(Restrictions.idEq(id));
			List list = getHibernateTemplate().findByCriteria(criteria);
			if (!list.isEmpty()) {
				return list.get(0);
			}
			return null;
		}
		catch (HibernateException e) {
			throw new DAOException(e.getMessage(), e);
		}
	}

	public void delete(Object object) throws DAOException {
		try {
			getHibernateTemplate().delete(object);
		}
		catch (HibernateException e) {
			throw new DAOException(e.getMessage(), e);
		}
	}

	public void deleteAll(Collection collection) throws DAOException {
		try {
			getHibernateTemplate().deleteAll(collection);
		}
		catch (HibernateException e) {
			throw new DAOException(e.getMessage(), e);
		}
	}

	public void save(Object object) throws DAOException {
		try {

			getHibernateTemplate().save(object);

		}
		catch (HibernateException e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage(), e);
		}
	}

	public void saveOrUpdateAll(Collection collection) throws DAOException {
		try {
			getHibernateTemplate().saveOrUpdateAll(collection);
		}
		catch (HibernateException e) {
			throw new DAOException(e.getMessage(), e);
		}
	}

	public void update(Object object) throws DAOException {
		try {
			getHibernateTemplate().update(object);
		}
		catch (HibernateException e) {
			throw new DAOException(e.getMessage(), e);
		}
	}

}
